package com.ys.controller.web.account;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.account.TcwNeedPayAccount;
import com.ys.entity.account.TcwNeedPayLedgerDetail;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/account/tcwNeedPayLedgerDetail")
public class WebTcwNeedPayLedgerDetailController {
	
	@Autowired
	private CommonApiService<TcwNeedPayLedgerDetail> commonApiService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, String needPayAccountCode){

		try {
			Map<String, Object> extraParam = new HashMap<String, Object>();
			if (needPayAccountCode != null && needPayAccountCode.length() > 0) {
				TcwNeedPayAccount tcwNeedPayAccount = new TcwNeedPayAccount().setCode(needPayAccountCode).queryFirst();
				extraParam.put("needPayAccountName", tcwNeedPayAccount.getName());
			}
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_NeedPayLedgerDetail, new ArrayList<Map<String, Object>>());
			
			TcwNeedPayLedgerDetail entity = new TcwNeedPayLedgerDetail()
					.setNeedPayAccountCode(needPayAccountCode)
					.setCreateTime(new Date());
			return commonApiService.toAdd(model, entity, MenuId.TCW_NeedPayLedgerDetail, extraParam, subGridConfig);
			
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("tcwNeedPayLedgerDetail:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		if (id == null) {
			return W.error(model, "单号不能为空");
		}
		
		Map<String, Object> extraParam = new TcwNeedPayLedgerDetail().setId(id)
				.addField("A.State state")
				.addField("A.TransferStatus transferStatus")
				.addField("(A.CostPrice + A.ServicePrice) * A.Quantity totalPrice")
				.addField("(A.CostPrice * A.Quantity - A.PayedPrice) unpayPrice")
				.addField("(A.ProxyPrice) * A.Quantity proxyTotalPrice")
				.addLeftJoin("TCW_NeedPayAccount B ON B.Code = A.NeedPayAccountCode")
	    		.addField("B.Name needPayAccountName")
	    	
	    		.addLeftJoin("THR_OperateCompanyApply B1 ON B1.Vgsbm = B.Vgsbm")
	    		.addField("B1.Vgsbm needPayAccountVgsbm")
	    		.addField("B1.Vgsjc needPayAccountVgsjc")
	    	
	    		.addLeftJoin("TB_NeedPayType C ON C.Code = A.NeedPayTypeCode")
	    		.addField("C.Name needPayTypeName")
			
	    		.addLeftJoin("TS_UserChange D ON D.Id = A.CreateChangeId")
				.addField("D.UserId createUserId")
				.addField("D.DeptName createOrgName")
	    	
	    		.addLeftJoin("THR_OperateCompanyApply E ON E.Vgsbm = A.Vgsbm")
	    		.addField("E.Vgsjc vgsjc")
				
				.clearFieldsSource()
				.queryFirstMap();
		
		try {
			SubGridConfigDto subGridConfig = null;
			Long menuId =  MenuId.TCW_NeedPayLedgerDetail;
			if(extraParam.get("transferStatus")!=null && extraParam.get("transferStatus").toString().equals("1")){//已划拨
				menuId =  MenuId.TCW_NeedPayLedgerDetailByTransferStatus;
			}
			if(extraParam.get("state")!=null && extraParam.get("state").toString().equals("1")){//已完成
				menuId =  MenuId.TCW_NeedPayLedgerDetailByState;
			}
			commonApiService.toEdit(model, id, new TcwNeedPayLedgerDetail(), menuId, extraParam, subGridConfig);
			return "account/tcwNeedPayLedgerDetail/toEdit";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}